﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using BOHVote.Common;
using BOHVote.BLL;
 
using System.Data;
using BOHVote.Entity;

namespace BOH.Information
{
    public partial class ModifyDocuments : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.QueryString["ID"] != null)
                {
                    labFile.Visible = true;
                    FillForm(Request.QueryString["ID"].ToString());
                }
                else
                {
                    labFile.Visible = false;
                }
            }
        }

        protected void FillForm(string NotifyID)
        {
            DataSet dst = NotifyClass.getNotifyFileByID(NotifyID);
            if (dst.Tables.Count > 0 && dst.Tables[0].Rows.Count > 0)
            {
                txtDocumentTitle.Text = dst.Tables[0].Rows[0]["Notify_title"].ToString();
                drpStatus.SelectedValue = dst.Tables[0].Rows[0]["Notify_status"].ToString();
                drpVerify.SelectedValue = dst.Tables[0].Rows[0]["Notify_Verify"].ToString();
                hideDocUpload.Value = dst.Tables[0].Rows[0]["Notify_Context"].ToString();
            }
        }

        protected void butSave_Click(object sender, EventArgs e)
        {
            int isSaved = saveDoc();
            if (isSaved == 0)
            {
                Response.Redirect("DocumentsManage.aspx");
            }
            else if(isSaved == 1)
            {
                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "confirmFile", "<script>alert('上传文件类型不正确.');</script>");
            }
            else
                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "confirm", "<script>alert('数据保存不成功');</script>");
        }

        protected int saveDoc()
        { 
            int returnValue = 0;
            try
            {
                // 1:error form Upload ; 2 : error form save data to db; 0 :all save success;
                string strDocName= UploadFile();
                if (string.IsNullOrEmpty(strDocName)&&!string.IsNullOrEmpty(hideDocUpload.Value))
                {
                    strDocName = hideDocUpload.Value;
                }
                if (!string.IsNullOrEmpty(strDocName) && strDocName.ToLower() != "filetypeerror")
                {
                    if (Session["user"] != null)
                    {
                        if (Request.QueryString["ID"] != null)
                            NotifyClass.UpdateNotifyFile(Request.QueryString["ID"].ToString(),this.txtDocumentTitle.Text, drpVerify.SelectedValue, drpStatus.SelectedValue, strDocName, ((BHUser)Session["user"]).UserID);
                        else
                            NotifyClass.InsertNotifyFile(this.txtDocumentTitle.Text, drpVerify.SelectedValue, drpStatus.SelectedValue, strDocName, ((BHUser)Session["user"]).UserID);
                    }
                }
                else
                {
                    returnValue = 1;
                }
            }
            catch (Exception ex)
            {
                returnValue = 2;
            }
            return returnValue;
        }

        protected string UploadFile()
        {
            string returnValue = string.Empty;
            try
            {
                string strFullFolderPath = common.getAppSetting("FolderforSavePath") + "\\File";
                string LoadFullPath = common.getAppSetting("FolderforLoadPath") + "/File";
                common.CreateFolder(strFullFolderPath);
                HttpFileCollection hfc = Request.Files;
                for (int i = 0; i < hfc.Count; i++)
                {
                    if (hfc.Keys[i].ToLower().Trim() == "docupload")
                    {
                        HttpPostedFile hpf = hfc[i];
                        if (hpf.ContentLength > 0)
                        {
                            string FileExtension = System.IO.Path.GetExtension(hpf.FileName);
                            string FileTypes = common.getAppSetting("UploadFileTypes");
                            string FileName = System.IO.Path.GetFileName(hpf.FileName);
                            if (FileTypes.IndexOf(FileExtension.ToLower()) > 0)
                            {
                                string FileFullPath = strFullFolderPath + "\\" + FileName;
                                if (File.Exists(FileFullPath))
                                {
                                    File.Delete(FileFullPath);
                                }
                                hpf.SaveAs(FileFullPath);
                                string LoadPath = LoadFullPath + "/" + FileName;
                                returnValue = FileName;
                            }
                            else
                            {
                                returnValue = "filetypeerror";
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                returnValue = string.Empty;
            }
            return returnValue;
        }


    }
}
